package com.hnevc.contentproviderdemo;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class NewsContentProvider extends ContentProvider {

    MySQLiteOpenHelper mOpenHelper ;

    private static UriMatcher sUriMatcher = new UriMatcher(-1);

    public static final int SUCCESS =1;

    public static final int NEWS_ITEM = 2;

    static {
        sUriMatcher.addURI("com.hnevc.contentproviderdemo","news", SUCCESS);
        sUriMatcher.addURI("com.hnevc.contentproviderdemo","news/#",NEWS_ITEM);
    }
    public NewsContentProvider() {


    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        int code = sUriMatcher.match(uri);
        if(code == SUCCESS){
            SQLiteDatabase db = mOpenHelper.getWritableDatabase();
            int num = db.delete("news",selection,selectionArgs);
            return num;
        }else{
            return  0;
        }
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int code = sUriMatcher.match(uri);
        if(code == SUCCESS){
            SQLiteDatabase db = mOpenHelper.getWritableDatabase();
            long rowId = db.insert("news", null, values);
            if(rowId>0){
                Uri newRowUri = ContentUris.withAppendedId(uri,rowId);
                return newRowUri;
            }
            db.close();
            return null;
        }
        else{
            return  null;
        }

    }

    @Override
    public boolean onCreate() {
        mOpenHelper = new MySQLiteOpenHelper(getContext());
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        int code = sUriMatcher.match(uri);
        Cursor cursor = null;
        switch (code){
            case SUCCESS: {
                SQLiteDatabase db = mOpenHelper.getReadableDatabase();
                cursor = db.query("news", new String[]{"_id", "author", "title", "content", "publishDate"}
                        , null, null, null, null, null);
                break;
            }
            case NEWS_ITEM: {
                String ids = uri.getLastPathSegment();
                SQLiteDatabase db = mOpenHelper.getReadableDatabase();
                cursor = db.query("news",new String[]{"_id", "author", "title", "content", "publishDate"}
                                        ,"_id=?",new String[]{ids},
                                        null,null,null);
                break;
            }

        }
        return cursor;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int code =sUriMatcher.match(uri);
        int num=0;
        switch (code){
            case NEWS_ITEM:
            {
                SQLiteDatabase db = mOpenHelper.getWritableDatabase();
                num = db.update("news",values,selection,selectionArgs);
                break;
            }
        }
        return num;

    }
}
